package com.example.teachingevaluationsystem.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.teachingevaluationsystem.entity.User;
import com.example.teachingevaluationsystem.entity.UserRole;
import com.example.teachingevaluationsystem.service.IUserRoleService;
import com.example.teachingevaluationsystem.service.IUserService;
import com.example.teachingevaluationsystem.vo.ChangePasswordVo;
import com.example.teachingevaluationsystem.vo.RegisterVo;
import com.example.teachingevaluationsystem.vo.UserVo;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;
import java.util.Arrays;
import java.util.List;

/**
 * 前端控制器
 *
 * @author 王妍
 * @since 2022-02-20
 */
@RestController
@RequestMapping(value = "/evaluate/user")
@AllArgsConstructor
public class UserController {
  private final IUserService userService;
  private final IUserRoleService userRoleService;

  @PostMapping("/registered")
  public void register(@RequestBody RegisterVo registerUser) {
    userService.register(registerUser);
  }

  @PutMapping("/password")
  public void changePassword(
      Principal principal, @RequestBody @Validated ChangePasswordVo changePasswordVo) {
    userService.changePassword(principal.getName(), changePasswordVo);
  }

  @GetMapping("/{userId}")
  public UserVo getUserInfo(@PathVariable("userId") Long userId) {
    final User user = userService.getById(userId);
    final UserVo userVo = new UserVo();
    BeanUtils.copyProperties(user, userVo);
    final Long[] roleIds =
        userRoleService
            .list(new LambdaQueryWrapper<UserRole>().eq(UserRole::getUserId, user.getId()))
            .stream()
            .map(UserRole::getRoleId)
            .toArray(Long[]::new);
    userVo.setRoleIds(roleIds);
    return userVo;
  }

  @DeleteMapping
  public boolean deleteByIds(@RequestBody Long[] ids) {
    return userService.removeBatchByIds(Arrays.asList(ids));
  }

  /**
   * 获取用户信息
   *
   */
  @GetMapping("info")
  public UserVo getUserInfo(Principal principal) {
    final UserVo userVo = new UserVo();
    final User user = userService.loadUserByUserName(principal.getName());
    BeanUtils.copyProperties(user,userVo);
    final Long[] roleIds =
            userRoleService
                    .list(new LambdaQueryWrapper<UserRole>().eq(UserRole::getUserId, user.getId()))
                    .stream()
                    .map(UserRole::getRoleId)
                    .toArray(Long[]::new);
    userVo.setRoleIds(roleIds);
    user.setPassword(null);
    return userVo;
  }

  /**
   * 查询用户信息列表
   *
   * @param page 分页条件
   * @param userVo user info 参数
   * @return 用户分页信息
   */
  @GetMapping("page")
  public Page<UserVo> loadPageUser(Page<UserVo> page, UserVo userVo) {
    final Page<UserVo> userVoPage = userService.loadUserVoPage(page, userVo);
    userVoPage.getRecords().forEach(userVo1 -> userVo1.setRoleIds( userRoleService
            .list(new LambdaQueryWrapper<UserRole>().eq(UserRole::getUserId, userVo1.getId()))
            .stream()
            .map(UserRole::getRoleId)
            .toArray(Long[]::new)));
    return userVoPage;
  }

  @DeleteMapping("/{userId}")
  public void deleteById(@PathVariable("userId") Long userId) {
    userService.removeById(userId);
  }

  @PutMapping
  public void update(@RequestBody UserVo userVo) {
    userService.saveInfo(userVo);
  }

  @GetMapping("teacher")
  public List<User> loadTeacherList() {
    return userService.loadTeacherList();
  }
}
